﻿using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using Limestone;
using Limestone.MVC;
using LimestoneMVC.Configuration;
using LimestoneMVC.Models;

namespace LimestoneMVC
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            RegisterComponents();

            // Important! Limestone needs this to properly model bind the different field types
            ModelBinders.Binders.Add(typeof(Field), new FieldBinder());

        }


        private void RegisterComponents()
        {
            var builder = new ContainerBuilder();

            // register the MVC controllers, Views and some autofac basics
            builder.RegisterControllers(typeof(MvcApplication).Assembly);
            builder.RegisterSource(new ViewRegistrationSource());
            builder.RegisterModule(new AutofacWebTypesModule());

            // register module dependencies
            builder.RegisterModule(new LimestoneModule());

            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}